package com.ruoyi.reportReview.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.reportReview.domain.bo.IndustrialReportInfoParkBo;
import com.ruoyi.reportReview.domain.vo.IndustrialReportInfoParkVo;
import com.ruoyi.reportReview.domain.IndustrialReportInfoPark;
import com.ruoyi.reportReview.mapper.IndustrialReportInfoParkMapper;
import com.ruoyi.reportReview.service.IIndustrialReportInfoParkService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 填报对象-园区信息Service业务层处理
 *
 * @author yangzhen
 * @date 2025-11-23
 */
@RequiredArgsConstructor
@Service
public class IndustrialReportInfoParkServiceImpl implements IIndustrialReportInfoParkService {

    private final IndustrialReportInfoParkMapper baseMapper;

    /**
     * 查询填报对象-园区信息
     */
    @Override
    public IndustrialReportInfoParkVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询填报对象-园区信息列表
     */
    @Override
    public TableDataInfo<IndustrialReportInfoParkVo> queryPageList(IndustrialReportInfoParkBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<IndustrialReportInfoPark> lqw = buildQueryWrapper(bo);
        Page<IndustrialReportInfoParkVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询填报对象-园区信息列表
     */
    @Override
    public List<IndustrialReportInfoParkVo> queryList(IndustrialReportInfoParkBo bo) {
        LambdaQueryWrapper<IndustrialReportInfoPark> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<IndustrialReportInfoPark> buildQueryWrapper(IndustrialReportInfoParkBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<IndustrialReportInfoPark> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getParkName()), IndustrialReportInfoPark::getParkName, bo.getParkName());
        lqw.eq(StringUtils.isNotBlank(bo.getParkType()), IndustrialReportInfoPark::getParkType, bo.getParkType());
        lqw.eq(StringUtils.isNotBlank(bo.getMainGoodsType()), IndustrialReportInfoPark::getMainGoodsType, bo.getMainGoodsType());
        lqw.eq(StringUtils.isNotBlank(bo.getOperatorCompany()), IndustrialReportInfoPark::getOperatorCompany, bo.getOperatorCompany());
        lqw.eq(StringUtils.isNotBlank(bo.getUnifiedSocialCode()), IndustrialReportInfoPark::getUnifiedSocialCode, bo.getUnifiedSocialCode());
        lqw.eq(bo.getArea() != null, IndustrialReportInfoPark::getArea, bo.getArea());
        lqw.eq(StringUtils.isNotBlank(bo.getBelongProject()), IndustrialReportInfoPark::getBelongProject, bo.getBelongProject());
        lqw.eq(bo.getStorageArea() != null, IndustrialReportInfoPark::getStorageArea, bo.getStorageArea());
        lqw.eq(bo.getColdArea() != null, IndustrialReportInfoPark::getColdArea, bo.getColdArea());
        lqw.eq(bo.getRailwayNum() != null, IndustrialReportInfoPark::getRailwayNum, bo.getRailwayNum());
        lqw.eq(StringUtils.isNotBlank(bo.getAddress()), IndustrialReportInfoPark::getAddress, bo.getAddress());
        lqw.eq(StringUtils.isNotBlank(bo.getLongitude()), IndustrialReportInfoPark::getLongitude, bo.getLongitude());
        lqw.eq(StringUtils.isNotBlank(bo.getLatitude()), IndustrialReportInfoPark::getLatitude, bo.getLatitude());
        lqw.eq(StringUtils.isNotBlank(bo.getManager()), IndustrialReportInfoPark::getManager, bo.getManager());
        lqw.eq(StringUtils.isNotBlank(bo.getManagerContact()), IndustrialReportInfoPark::getManagerContact, bo.getManagerContact());
        lqw.eq(bo.getDataStatus() != null, IndustrialReportInfoPark::getDataStatus, bo.getDataStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList1()), IndustrialReportInfoPark::getFileList1, bo.getFileList1());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList2()), IndustrialReportInfoPark::getFileList2, bo.getFileList2());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList3()), IndustrialReportInfoPark::getFileList3, bo.getFileList3());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList4()), IndustrialReportInfoPark::getFileList4, bo.getFileList4());
        lqw.eq(StringUtils.isNotBlank(bo.getFileList5()), IndustrialReportInfoPark::getFileList5, bo.getFileList5());
        return lqw;
    }

    /**
     * 新增填报对象-园区信息
     */
    @Override
    public Boolean insertByBo(IndustrialReportInfoParkBo bo) {
        IndustrialReportInfoPark add = BeanUtil.toBean(bo, IndustrialReportInfoPark.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改填报对象-园区信息
     */
    @Override
    public Boolean updateByBo(IndustrialReportInfoParkBo bo) {
        IndustrialReportInfoPark update = BeanUtil.toBean(bo, IndustrialReportInfoPark.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(IndustrialReportInfoPark entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除填报对象-园区信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
